
init();

/**
 * 界面初始化
 */
function init() {
    initMaterialFirstType();
}

/**
 * 一级材料类型加载
 */
function initMaterialFirstType(){
    $.ajax({
        url :   "http://localhost:8080/userCore/material/type/queryList",
        type:   "post",
        data:JSON.stringify({}),
        contentType:"application/json;",
        success:function(data) {
            console.log(data);
            var firstMaterialHtml = "";
            var list = data.data;
            for(var i=0; i<list.length; i++){
                firstMaterialHtml = firstMaterialHtml
                        +'<option style="text-align: center;" value="'
                        +list[i].typeCode
                        +'">'
                        +list[i].typeName
                        +'</option>';
            }
            $("#firstMaterial").html(firstMaterialHtml);
            if(list != null && list.length > 0){
                initMaterialTwoType(list[0].typeCode);
            }
        }
    });
}

/**
 * 一级材料类型监听事件
 */
$("#firstMaterial").change(function(){
    var typeCode = $("#firstMaterial").val();
    initMaterialTwoType(typeCode);
});

/**
 * 二级材料类型加载
 */
function initMaterialTwoType(typeCode){
    $.ajax({
        url :   "http://localhost:8080/userCore/material/type/queryList",
        type:   "post",
        data:JSON.stringify({typeCode:typeCode}),
        contentType:"application/json;",
        success:function(data) {
            console.log(data);
            var twoMaterialHtml = "";
            var list = data.data;
            for(var i=0; i<list.length; i++){
                twoMaterialHtml = twoMaterialHtml
                    +'<option style="text-align: center;" value="'
                    +list[i].typeId
                    +'">'
                    +list[i].typeName
                    +'</option>';
            }
            $("#twoMaterial").html(twoMaterialHtml);

            if(list != null && list.length > 0){
                queryMaterialList(list[0].typeId);
            }
        }
    });
}

/**
 * 二级材料类型监听事件
 */
$("#twoMaterial").change(function(){
    var typeId = $("#twoMaterial").val();
    queryMaterialList(typeId);
});

/**
 * 查询材料列表
 */
function queryMaterialList(typeId) {
    $.ajax({
        url :   "http://localhost:8080/userCore/material/queryList",
        type:   "post",
        data:JSON.stringify({materialTypeId:typeId}),
        contentType:"application/json;",
        success:function(data) {
            console.log(data);
        }
    });
}